﻿@{
    ViewBag.Title = "一般计划任务";
    Layout = "~/Views/Shared/PlanPartial/_LayoutDetail.cshtml";
}

@using Wicresoft.UnifyShow2.Portal.Extensions;
@using Wicresoft.UnifyShow.Business;

@{
    var range = ViewData["InspectionRange"] as List<PlanLocationOwner>;
    var currentUser = ViewBag.CurrentUser as User_XModel;

    var Model = ViewData["Model"] as ScheduleJob;
    var isView = Model != null && Model.Job != null;
    var job = isView ? Model.Job : null;
    var jobRange = (isView && job != null && job.JobRange != null) ? job.JobRange.Split(';').ToList() : null;
    var bulletin = isView ? Model.Bulletin : null;

    //创建人可以编辑计划
    var canEdit = (isView && currentUser.ID == job.JobOwnerID) || !isView;
}

@section Name{计划任务名称：}
@section Description{计划任务描述：}
@section CycleTitle{计划任务时间以及周期设置：}

@section InspectionRange
{
    <div class="parameter_name">计划任务范围：</div>
    <div id="generalRangeContainer">
        <input type="checkbox" id="checkAllRange" class="list_all" data-required-info="请选择计划任务范围!" name="generalRange" />
        <label for="checkAllRange">全选</label>
        @foreach (var item in range)
        {
            <input type="checkbox" id="range_@(item.LocationID)" name="generalRange" class="list_item" value="@(item.LocationID)" @((isView && jobRange != null && jobRange.Contains(item.LocationID.ToString())) ? "checked='checked'" : "") />
            <label for="range_@(item.LocationID)">@item.Alias</label>
        }
    </div>
}

@section FileUpload
{
    <div class="parameter_name">相关文件：</div>
    <div>
        <table id="FileList" class="" title="文件列表"
            fitcolumns="true" singleselect="true">
            <thead>
                <tr>
                    <th field="ID" hidden="true"></th>
                    <th field="FileName" width="160" formatter="addLink">文件名称</th>
                    <th field="UploadTime" width="160" formatter="DateFormate">上传时间</th>
                    <th field="UpLoadUserID" hidden="true"></th>
                    <th field="UpLoadUserName" width="160">上传人</th>
                    @if (canEdit)
                    {
                        <th field="Ope" width="160" formatter="OpeRender">操作</th>
                    }
                </tr>
            </thead>
        </table>

        @if (canEdit)
        {
            <iframe name="targetUpload" src="/PlanManagement/FileUpload_Job" border="0" frameborder="0" style="border: none;height:70px;"></iframe>
        }
    </div>
}

@section ElectronicBulletin
{
    <div class="parameter_name">
        <input type="checkbox" id="electronicBulletin" onclick="toggleBulletinEnable();" @((isView && job.HasBulletin == 1) ? "checked='checked'" : "") />
        <label for="electronicBulletin">电子公告</label>
    </div>
    <div class="outer_border" id="bulletinDetail" style="padding: 20px;">
        <div class="parameter">
            <div class="parameter_name"><span class="required">*</span>开始时间：</div>
            <div class="parameter_input">
                <input type="text" id="bulletinStartDate" class="easyui-datebox startDate" value="@(isView && job.HasBulletin == 1 && bulletin != null ? bulletin.BeginTime.ToString("yyyy-MM-dd") : "")" />
            </div>
        </div>
        <div class="parameter">
            <div class="parameter_name"><span class="required">*</span>失效时间：</div>
            <div class="parameter_input">
                <input type="text" id="bulletinEndDate" class="easyui-datebox endDate" value="@(isView && job.HasBulletin == 1 && bulletin != null ? bulletin.EndTime.ToString("yyyy-MM-dd") : "")" />
            </div>
        </div>
        <div class="parameter one_line">
            <div class="parameter_name"><span class="required">*</span>标题：</div>
            <div class="parameter_input">
                <input type="text" id="bulletinTitle" value="@(bulletin != null ? bulletin.Title : "")" />
            </div>
        </div>
        <div class="parameter one_line">
            <div class="parameter_name"><span class="required">*</span>内容：</div>
            <div class="parameter_input">
                <textarea rows="5" id="bulletinContent">@(bulletin != null ? bulletin.Content : "")</textarea>
            </div>
        </div>
    </div>
}

<script type="text/javascript">

    var DateFormate = function (value, row, index) {
        if (value) {
            return value.DateStringConvert('yyyy-MM-dd');
        }
    };

    var OpeRender = function (value, row, index) {
        //if (value != true) return "";
        return '<a class="deleteRow">删除</a>';
    };

    var addLink = function (value, row, index) {
        if (value) {
            var path = row["UpLoadPath"]
            return '<a href="' + path + '" download>' + value + '</a>';
        }
    };


    var TaskD = {};
    TaskD.AppendFile = function (fileName) {
        if (fileName) {
            window.setTimeout(function () {
                $('#FileList').datagrid('reload');
            }, 300);
        }
    };

    //电子公告是否可编辑
    var toggleBulletinEnable = function () {
        var isChecked = $('#electronicBulletin').is(':checked'),
            container = $('#bulletinDetail'),
            inputs = container.find('#bulletinTitle,#bulletinContent');

        container.find('.easyui-datebox').each(function (i, obj) {
            var val = $(obj).datebox('getValue');
            $(obj).datebox({ 'disabled': !isChecked });
            $(obj).datebox('setValue', val);
        });

        if (isChecked) {
            inputs.removeAttr('disabled');
        } else {
            inputs.attr('disabled', true);
        }

        container.find('.validatebox-text').attr('disabled', true);
    };

    var saveBulletin = function () {
        var data = {};

        data["Title"] = $('#bulletinTitle').val();
        data["Content"] = $('#bulletinContent').val();
        data["BeginTime"] = $('#bulletinStartDate').datebox('getValue');
        data["EndTime"] = $('#bulletinEndDate').datebox('getValue');

        return data;
    };

    var SavePlan = function () {

        var data = {};

        data["JobType"] = 3;

        var range = "";
        $('[name="generalRange"]:checked:not("#checkAllRange")').each(function () {
            range += $(this).val() + ";";
        });
        data["JobRange"] = range;

        data["HasBulletin"] = $('#electronicBulletin').is(':checked') ? 1 : 0;
        if (data["HasBulletin"] == 1) {
            data["bulletin"] = saveBulletin();
        }

        planSave(data);
    };

    $(function () {
        var canEdit = $('#canEdit').val() == "True";
        if (canEdit) {
            window.setTimeout(function () {
                enableControl('generalRangeContainer');
            }, 0);
        }

        $('#FileList').datagrid({
            url: '/PlanManagement/GetJobFileList',
            onClickCell: function (rowIndex, field, value) {
                if (field == "Ope") {
                    $.messager.confirm('提醒', '确认删除这个文件吗？', function (r) {
                        if (r) {
                            $.post('/PlanManagement/FileDelete', {
                                ID: $('#FileList').datagrid('getData').rows[rowIndex]["ID"]
                            }, function (res) {
                                if (res == "OK") {
                                    $.messager.alert('提示', '删除成功!', 'info');
                                    $('#FileList').datagrid('reload');
                                }
                                if (res == "Error") {
                                    $.messager.alert('错误', '删除失败!', 'error');
                                }
                            });
                            $('#FileList').datagrid('deleteRow', rowIndex);
                        }
                    });
                }
            }
        });

        //var fileDetail = $('#fileListDetail').val();
        //if (fileDetail != "") {
        //    var list = JSON.parse(fileDetail);
        //    $.each(list, function (i, val) {
        //        val["UploadTime"] = DateFormate(val["UploadTime"]);
        //    });
        //    $('#FileList').datagrid({ data: list });
        //}
    });
</script>

@if (!isView)
{ 
    <script type="text/javascript">
        $(function () {

            var today = new Date(), endDate = new Date();
            endDate.setDate(today.getDate() + 7);
            $('#bulletinEndDate').datebox('setValue', endDate.formate());

            toggleBulletinEnable();

        });
    </script>
}
